﻿using System.Runtime.CompilerServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.GraphicsInterface;

namespace ChangDiTools.Tool {

    /// <summary>
    /// 字体字符集名称
    /// </summary>
    public enum FontCharSet {
        /// <summary>
        /// 英文
        /// </summary>
        Ansi = 0,
        /// <summary>
        /// 与当前操作系统语言有关，如为简体中文，则被设置为GB2312
        /// </summary>
        Default = 1,
        /// <summary>
        /// 简体中文
        /// </summary>
        GB2312 = 134,
        /// <summary>
        /// 繁体中文
        /// </summary>
        Big5 = 136,
        /// <summary>
        /// 与操作系统有关
        /// </summary>
        OEM = 255
    }

    /// <summary>
    /// 字体的字宽
    /// </summary>
    public enum FontPitch {
        /// <summary>
        /// 默认字宽
        /// </summary>
        Default = 0,
        /// <summary>
        /// 固定字宽
        /// </summary>
        Fixed = 1,
        /// <summary>
        /// 可变字宽
        /// </summary>
        Variable = 2
    }

    /// <summary>
    /// 字体的语系定义
    /// </summary>
    public enum FontFamily {
        /// <summary>
        /// 使用默认字体
        /// </summary>
        Dontcare = 0,
        /// <summary>
        /// 可变的笔划宽度，有衬线，如MS Serif字体
        /// </summary>
        Roman = 16,
        /// <summary>
        /// 可变的笔划宽度，无衬线，如MS Sans Serif字体 
        /// </summary>
        Swiss = 32,
        /// <summary>
        /// 固定笔划宽度，衬线可以有也可以没有,如Courier New字体
        /// </summary>
        Modern = 48,
        /// <summary>
        /// 手写体，如Cursive字体
        /// </summary>
        Script = 64,
        /// <summary>
        /// 小说字体，如旧式英语
        /// </summary>
        Decorative = 80
    }
    public static class TextStyleTools {
        /// <summary>
        /// 创建一个新的文字样式
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <param name="styleName">文字样式名</param>
        /// <param name="fontFileName">字体文件名</param>
        /// <param name="bigFontFileName">大字体文件名</param>
        /// <returns>返回添加的文字样式的ID</returns>
        public static ObjectId AddTextStyle(this Database db, string styleName, string fontFileName,
            string bigFontFileName) {
            TextStyleTable tst = db.TextStyleTableId.GetObject(OpenMode.ForRead) as TextStyleTable;
            if (!tst.Has(styleName)) {
                TextStyleTableRecord str = new TextStyleTableRecord {
                    Name = styleName,
                    FileName = fontFileName,
                    BigFontFileName = bigFontFileName
                };
                tst.UpgradeOpen();
                tst.Add(str);
                db.TransactionManager.AddNewlyCreatedDBObject(str, true);
                tst.DowngradeOpen();
            }
            return tst[styleName];
        }
        //创建一个未定义大字体的文字样式
        public static ObjectId AddTextStyle(this Database db, string styleName, string fontFileName)
        {
            return db.AddTextStyle(styleName, fontFileName, "");
        }
        /// <summary>
        /// 创建一个新的文字样式
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <param name="styleName">文字样式名</param>
        /// <param name="fontName">字体文件名</param>
        /// <param name="bold">是否加粗</param>
        /// <param name="italic">是否倾斜</param>
        /// <param name="charset">字体的字符集</param>
        /// <param name="pitchAndFamily">字体的字宽和语系定义</param>
        /// <returns>返回添加的文字样式的Id</returns>
        public static ObjectId AddTextStyle(this Database db, string styleName, string fontFileName, bool bold,
            bool italic, int charset, int pitchAndFamily)
        {
            TextStyleTable st=db.TextStyleTableId.GetObject(OpenMode.ForRead) as TextStyleTable;
            if (!st.Has(styleName))
            {
                TextStyleTableRecord str=new TextStyleTableRecord()
                {
                    Name = styleName,
                    Font = new FontDescriptor(fontFileName,bold,italic,charset,pitchAndFamily)
                };
                st.UpgradeOpen();
                st.Add(str);
                db.TransactionManager.AddNewlyCreatedDBObject(str,true);
                st.DowngradeOpen();
            }

            return st[styleName];
        }
        /// <summary>
        /// 添加包含图案的形定义文件
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <param name="styleName">样式名</param>
        /// <param name="shapeFilename">形定义文件名</param>
        /// <returns>返回新添加的文字样式表记录的ObjectId</returns>
        public static ObjectId AddShapeTextStyle(this Database db, string styleName, string shapeFileName)
        {
            TextStyleTable st = db.TextStyleTableId.GetObject(OpenMode.ForRead) as TextStyleTable;
            if (!st.Has(styleName)) {
                TextStyleTableRecord str = new TextStyleTableRecord() {
                    Name = styleName,
                    FileName = shapeFileName,
                    IsShapeFile = true
                };
                st.UpgradeOpen();
                st.Add(str);
                db.TransactionManager.AddNewlyCreatedDBObject(str, true);
                st.DowngradeOpen();
            }

            return st[styleName];
        }
        /// <summary>
        /// 设置当前文字样式
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <param name="styleName">文字样式名</param>
        /// <returns>如果设置成功返回true，否则返回false</returns>
        public static bool SetCurrentTextStyle(this Database db, string styleName)
        {
            var tr = db.TransactionManager;
            TextStyleTable st=tr.GetObject(db.TextStyleTableId,OpenMode.ForRead) as TextStyleTable;
            if (!st.Has(styleName)) return false;
            ObjectId styleId = st[styleName];
            if (db.Textstyle == styleId) return false;
            db.Textstyle = styleId;
            return true;
        }
        /// <summary>
        /// 删除文字样式
        /// </summary>
        /// <param name="db">数据库对象</param>
        /// <param name="styleName">文字样式名</param>
        /// <returns>如果删除成功返回true，否则返回false</returns>
        public static bool DeleteTextStyle(this Database db, string styleName)
        {
            var tr = db.TransactionManager;
            TextStyleTable st=tr.GetObject(db.TextStyleTableId,OpenMode.ForRead) as TextStyleTable;
            if (!st.Has(styleName)) return false;
            ObjectId styleId = st[styleName];
            if (db.Textstyle == styleId) return false;//不能删除当前文字样式
            TextStyleTableRecord str=tr.GetObject(styleId,OpenMode.ForWrite) as TextStyleTableRecord;
            str.Erase();
            return true;
        }

        public static void SetTextStyleProp(this ObjectId styleId, double textSize, double xScale, double obliquingangle,
            bool isVertical, bool upsideDown, bool backwards, AnnotativeStates annotative, bool paperOrientation)
        {
            TextStyleTableRecord str=styleId.GetObject(OpenMode.ForWrite) as TextStyleTableRecord;
            if(str==null) return;
            str.TextSize = textSize;
            str.XScale = xScale;
            str.ObliquingAngle = obliquingangle;
            str.IsVertical = isVertical;
            str.FlagBits=(byte)0;
            str.FlagBits += upsideDown ? (byte) 2 : (byte) 0;
            str.FlagBits += backwards ? (byte) 4 : (byte) 0;
            str.Annotative = annotative;
            str.SetPaperOrientation(paperOrientation);
            str.DowngradeOpen();
        }
    }
}
